package pers.li.service;

import pers.li.dao.AddressDao;
import pers.li.dao.GoodsDao;
import pers.li.dao.OrderDao;
import pers.li.model.Address;
import pers.li.model.Goods;
import pers.li.model.Order;
import pers.li.model.OrderDetail;
import pers.li.model.TO.OrderGoodTO;
import pers.li.model.TO.OrderTO;
import pers.li.model.VO.OrderDetailVO;
import pers.li.model.VO.OrderVO;
import pers.li.utils.BeanCopy;

import java.util.ArrayList;
import java.util.List;

public class OrderService {
    private final OrderDao orderDao = new OrderDao();
    private final GoodsDao goodsDao = new GoodsDao();
    private final AddressDao addressDao = new AddressDao();
    public void insertOrder(OrderTO orderTO) {
        List<OrderGoodTO> goods = orderTO.getGoods();
        for (OrderGoodTO good : goods) {
            int orderId = orderDao.insertOrder(orderTO, good);
            orderDao.insertOrderDetail(orderId, good);
            for (Integer contentId : good.getContentId()) {
                orderDao.insertOrderContent(orderId, contentId);
            }
        }
    }

    public List<OrderVO> getOrderVOListByUserId(Integer userId) {
        List<Order> orderList = orderDao.getOrderListByUserId(userId);
        if (orderList != null) {
            List<OrderVO> orderVOList = new ArrayList<>();
            for (Order order : orderList) {
                OrderVO orderVO = new OrderVO();
                BeanCopy.copy(order, orderVO);
                OrderDetail orderDetail = orderDao.getOrderDetailByOrderId(order.getOrderId());
                BeanCopy.copy(orderDetail, orderVO);
                Goods goods = goodsDao.getGoodsByGoodId(order.getGoodsId());
                orderVO.setGoodsPic(goods.getGoodsPic());
                orderVO.setGoodName(goods.getGoodsName());
                List<String> contentName = orderDao.getContentNameByOrderId(order.getOrderId());
                orderVO.setContentName(contentName);
                orderVOList.add(orderVO);
            }
            return orderVOList;
        }
        return null;
    }

    public void payOrder(Integer orderId) {
        orderDao.updateOrderStatus(orderId, 1);
    }

    public OrderDetailVO getOrderDetailVOByOrderId(Integer orderId) {
        Order order = orderDao.getOrderByOrderId(orderId);
        OrderDetailVO orderDetailVO = new OrderDetailVO();
        BeanCopy.copy(order, orderDetailVO);
        OrderDetail orderDetail = orderDao.getOrderDetailByOrderId(order.getOrderId());
        BeanCopy.copy(orderDetail, orderDetailVO);
        Goods goods = goodsDao.getGoodsByGoodId(order.getGoodsId());
        orderDetailVO.setGoodsPic(goods.getGoodsPic());
        orderDetailVO.setGoodName(goods.getGoodsName());
        List<String> contentName = orderDao.getContentNameByOrderId(order.getOrderId());
        orderDetailVO.setContentName(contentName);
        Address address = addressDao.getAddress(order.getAddrId());
        orderDetailVO.setAddress(address);
        return orderDetailVO;
    }

    public static void main(String[] args) {
        System.out.println(new OrderService().getOrderDetailVOByOrderId(1));
    }
}
